package com.msxt.context;

public class BaseContext {
    private static final ThreadLocal<UserContext> contextHolder = new ThreadLocal<>();

    public static void setUserContext(UserContext userContext) {
        contextHolder.set(userContext);
    }

    public static UserContext getUserContext() {
        return contextHolder.get();
    }

    public static Long getCurrentId() {
        UserContext context = getUserContext();
        return context != null ? context.getUserId() : null;
    }

    public static String getTableName() {
        UserContext context = getUserContext();
        return context != null ? context.getTableName() : null;
    }

    public static String getUsername() {
        UserContext context = getUserContext();
        return context != null ? context.getUsername() : null;
    }

    public static String getRole() {
        UserContext context = getUserContext();
        return context != null ? context.getRole() : null;
    }

    public static void clearContext() {
        contextHolder.remove();
    }

    public static class UserContext {
        private final Long userId;
        private final String role;
        private final String tableName;
        private final String username;

        public UserContext(Long userId, String role, String tableName, String username) {
            this.userId = userId;
            this.role = role;
            this.tableName = tableName;
            this.username = username;
        }

        public Long getUserId() { return userId; }
        public String getRole() { return role; }
        public String getTableName() { return tableName; }
        public String getUsername() { return username; }
    }
}